package com.example.userclient.fragment

import android.os.Bundle
import android.util.SparseArray
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat.getColor
import androidx.databinding.DataBindingUtil
import com.example.userclient.MyApplication
import com.example.userclient.adapter.MyFragmentAdapter
import com.example.userclient.R
import com.example.userclient.databinding.FragmentBottomBinding
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [BottomFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class BottomFragment : Fragment() {
    // TODO: Rename and change types of parameters

    private var binding :FragmentBottomBinding?= null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val bind = DataBindingUtil.inflate<FragmentBottomBinding>(inflater, R.layout.fragment_bottom,container,false)
        binding = bind
        return bind.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        val fragments = SparseArray<Fragment>()
        fragments.put(0, ShopFragment.newInstance())
        fragments.put(1, MineFragment.newInstance())
        val myFragmentAdapter = MyFragmentAdapter(this,fragments)
        binding!!.pager.adapter = myFragmentAdapter
//        binding!!.bottomTabLayout.addTab(binding)
        val tabLayout = binding!!.bottomTabLayout
        tabLayout.tabMode = TabLayout.MODE_FIXED
        tabLayout.tabGravity = TabLayout.GRAVITY_FILL
        tabLayout.setTabTextColors( getColor(MyApplication.instance, R.color.black), getColor(MyApplication.instance, R.color.teal_700))
        TabLayoutMediator(tabLayout, binding!!.pager) { tab, position ->
            when(position){
                0->
                    tab.setText("附近").setIcon(R.drawable.ic_publish_goods_fill)
                1->
                    tab.setText("我的").setIcon(R.drawable.ic_mine)
                else->{
                    throw Exception("no handler")
                }
            }
        }.attach()


    }

    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment BottomFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance() =
            BottomFragment()
    }
}